/**
 * 
 */
package com.telenav.techtalk;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @author yxyao
 *
 */
public class AlphaViewGroupActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bitmap hawksbay = BitmapFactory.decodeResource(getResources(), R.drawable.hawks_bay);
        ImageView iv = new ImageView(this);
        iv.setImageBitmap(hawksbay);
        FrameLayout fl = new FrameLayout(this);
        setContentView(fl, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        fl.addView(iv, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.TOP|Gravity.LEFT));
        AlphaLinearLayout alphaLl = new AlphaLinearLayout(this);
        fl.addView(alphaLl);
        LayoutInflater inflator = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        inflator.inflate(R.layout.alpha_sample, alphaLl);
    }
    
    class AlphaLinearLayout extends LinearLayout{

        private int alpha;
        /**
         * @param context
         */
        public AlphaLinearLayout(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
        
        protected void dispatchDraw(Canvas canvas) {
            alpha +=2;
            if(alpha>255)
            {
                alpha=0;
            }
            int saveCount = canvas.saveLayerAlpha(0, 0, getWidth(), getHeight(), alpha, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
            try{
                super.dispatchDraw(canvas);
            }finally
            {
                canvas.restoreToCount(saveCount);
            }
            invalidate();
        }
        
    }
    
}
